import { EAlnumType } from './../enums/common.enum';
import { PicsService } from './pics.service';
import {
  Controller,
  Post,
  Body,
  UploadedFile,
  UseInterceptors,
  Get,
  Param,
  Query,
  Delete,
  Header,
  Response,
} from '@nestjs/common';
import {
  GetPicListDto,
  ConfigPicDto,
  DownloadDto,
  GetPicsByTypeDto,
} from './dto';
import { FileInterceptor } from '@nestjs/platform-express';
import { createWriteStream, write } from 'fs';
import { join } from 'path';
import { ApiTags, ApiOperation } from '@nestjs/swagger';
const fs = require('fs');
@ApiTags('图片管理')
@Controller('pics')
export class PicsController {
  constructor(private readonly picService: PicsService) {}
  @Post('getPicList')
  @ApiOperation({ description: '分页获取图片' })
  async getPicList(@Body() body: GetPicListDto) {
    try {
      const res = await this.picService.getPicList(body);
      return res;
    } catch (e) {
      console.log(e);
      return Promise.reject(e);
    }
  }
  // 新增/修改
  @ApiOperation({ description: '配置图片' })
  @Post('configPic')
  async configPic(@Body() body: ConfigPicDto) {
    try {
      const res = await this.picService.configPic(body);
      return res;
    } catch (e) {
      return Promise.reject(e);
    }
  }
  @Post('uploadImg')
  @UseInterceptors(FileInterceptor('file'))
  @ApiOperation({ description: '图片上传' })
  @Header('Content-Type', 'multipart/form-data')
  async uploadImg(@UploadedFile() file, @Response() res) {
    try {
      const path = join(__dirname, '..', 'upload');
      const time = Date.now() + '-' + file.originalname;
      if (!fs.existsSync(path)) {
        fs.mkdirSync(`${path}`);
      }
      const writer = createWriteStream(join(__dirname, '..', 'upload', time));
      writer.write(file.buffer);
      writer.close();

      writer.on('finish', () => {
        res.json({
          path: '/upload/' + time,
        });
      });
    } catch (e) {
      console.log(e);
      return Promise.reject(e);
    }
  }
  @ApiOperation({ description: '图片下载' })
  @Post('download')
  async download(@Body() body: DownloadDto) {
    try {
      const path = await this.picService.download(body);
      return path;
    } catch (e) {
      console.log(e);
      return Promise.reject(e);
    }
  }
  @ApiOperation({ description: '获取图片详细' })
  @Get('getPicDetail')
  async getPicDetail(@Query('id') id: number) {
    try {
      return await this.picService.getDetail(id);
    } catch (e) {
      console.log(e);
      return Promise.reject(e);
    }
  }
  @ApiOperation({ description: '删除图片' })
  @Delete('delPic/:id')
  async delPic(@Param('id') id: number) {
    try {
      await this.picService.delPic(id);
      return 'success';
    } catch (e) {
      console.log(e);
      return Promise.reject(e);
    }
  }
  @ApiOperation({ description: '根据相册类型获取相关图片' })
  @Post('getPicsByType')
  async getPicsByType(@Body() body: GetPicsByTypeDto) {
    try {
      const { type } = body;
      switch (type) {
        case EAlnumType.BY_AUTHOR: {
        }
        case EAlnumType.BY_CLASSIFY: {
          return await this.picService.getPicsByClassify(body);
        }
      }
    } catch (e) {
      return Promise.reject(e);
    }
  }
}
